<?php
/**
* @file article.php
* Systemblock Article
*
* @author PortaMx - Portal Management Extension
* @author Copyright 2008-2011 by PortaMx - http://portamx.com
* @Version: 1.0 RC4
* @Date: 23.02.2011
*/

if(!defined('PortaMx'))
	die('This file can\'t be run without PortaMx');

/**
* @class pmxc_article
* Systemblock Article
* @see article.php
* @author Copyright by PortaMx - http://portamx.com
*/
class pmxc_article extends PortaMxC_SystemBlock
{
	var $articles;
	var $php_content;
	var $php_vars;

	/**
	* checkCacheStatus.
	* Article trigger do nothing.
	*/
	function pmxc_checkCacheStatus()
	{
		return true;
	}

	/**
	* InitContent.
	* Checks the cache status and create the content.
	*/
	function pmxc_InitContent()
	{
		global $context, $smcFunc, $pmxCacheFunc;

		// if visible init the content
		if($this->visible)
		{
			// called from static article block?
			if(!empty($this->cfg['config']['static_block']))
			{
				$this->cfg['name'] = $this->cfg['config']['settings']['article'];

				if($this->cfg['config']['settings']['usedframe'] == 'article')
					$this->cfg['config']['skip_outerframe'] = true;
				$this->cfg['config']['visuals']['bodytext'] = '';

				if(!empty($this->cfg['cache']))
					$this->articles = $pmxCacheFunc['get']($this->cache_key, $this->cache_mode);
				else
					$this->articles = array();
			}

			// requested
			else
			{
				$this->cache_key = 'req'. $this->cache_key;
				$this->articles = array();
				$this->cfg['config']['skip_outerframe'] = true;
				$this->cfg['config']['settings']['usedframe'] = 'article';
			}

			// get the articles
			if(empty($this->articles))
			{
				if(!empty($this->cfg['name']))
				{
					$request = $smcFunc['db_query']('', '
						SELECT a.id, a.name, a.acsgrp, a.ctype, a.config, a.owner, a.active, a.created, a.updated, a.content, m.member_name
						FROM {db_prefix}portamx_articles AS a
						LEFT JOIN {db_prefix}members AS m ON (a.owner = m.id_member)
						WHERE a.name = {string:art} AND a.active > 0 AND a.approved > 0
						ORDER BY a.id',
						array('art' => $this->cfg['name'])
					);

					if($smcFunc['db_num_rows']($request) > 0)
					{
						while($row = $smcFunc['db_fetch_assoc']($request))
						{
							$row['config'] = unserialize($row['config']);
							if(!empty($this->cfg['config']['settings']['inherit_acs']) || allowPmxGroup($row['acsgrp']))
							{
								// have a custom cssfile, load
								if(!empty($row['config']['cssfile']))
									$this->getCustomCSS($row);
								else
									$row['customclass'] = '';

								$row['side'] = $this->cfg['side'];
								$row['blocktype'] = (!empty($this->cfg['config']['static_block']) ? 'static_article' : 'article');
								$this->articles[] = $row;
							}
						}
						$smcFunc['db_free_result']($request);
					}

					// static block?
					if(!empty($this->cfg['config']['static_block']) && !empty($this->cfg['cache']))
						$pmxCacheFunc['put']($this->cache_key, $this->articles, $this->cache_time, $this->cache_mode);
				}
			}

			// articles found?
			if(count($this->articles) > 0)
			{
				// requested ?
				if(!empty($this->cfg['config']['static_block']))
				{
					$this->cfg['blocktype'] = 'artblock';
					$this->cfg['uniID'] = 'blk'. $this->cfg['id'];
				}
				else
				{
					$this->cfg['uniID'] = 'art'. $this->articles[0]['id'];
					$context['pmx']['pagenames']['art'] = htmlspecialchars($this->getUserTitle($this->articles[0]), ENT_QUOTES);
					if(empty($context['pmx']['pagenames']['art']))
						$context['pmx']['pagenames']['art'] = htmlspecialchars($this->articles[0]['name'], ENT_QUOTES);
				}

				if(!isset($_GET['cat']) && empty($this->cfg['config']['static_block']) && !empty($context['pmx']['settings']['topfragment']))
					$context['pmx']['html_footer'] .= '
						window.location.href = window.location.href.replace(/#topart'. $this->articles[0]['id'] .'/g, "") + "#topart'. $this->articles[0]['id'] .'";';
			}
			else
				$this->visible = false;

			if(!empty($this->visible))
			{
				// check for special php content
				foreach($this->articles as $art)
					if($art['ctype'] == 'php' && preg_match('~\[\?pmx_initphp(.*)pmx_initphp\?\]~is', $art['content'], $match))
						eval($match[1]);
			}
		}
		return $this->visible;
	}

	/**
	* ShowContent
	*/
	function pmxc_ShowContent()
	{
		if(!empty($this->cfg['uniID']))
			echo '
						<a name="top'. $this->cfg['uniID'] .'"></a>';

		$count = count($this->articles);
		foreach($this->articles as $cnt => $article)
		{
			if($this->cfg['config']['settings']['usedframe'] == 'block')
			{
				$article['config']['collapse'] = 0;
				$article['config']['visuals']['bodytext'] = '';
				if(count($this->articles) == 1)
				{
					$article['config']['visuals']['header'] = 'none';
					$article['config']['visuals']['frame'] = '';
					$article['config']['visuals']['body'] = '';
				}
				else
					$article['config']['visuals']['frame'] = 'roundframe';

				if(count($this->articles) > 1)
					$count--;
			}

			$artdata = Pmx_Frame_top($article, $count);
			$this->WriteContent($article);
			Pmx_Frame_bottom($article, $artdata);
		}
	}

	/**
	* Write out the Content
	*/
	function WriteContent($article)
	{
		global $context, $scripturl, $txt;

		$statID = 'art'. $article['id'] . $this->cfg['side'];
		$tease = 0;
		if($article['ctype'] == 'bbc')
		{
			$article['content'] = PortaMx_BBCsmileys(parse_bbc($article['content'], false));
			$tease = $article['config']['settings']['teaser'];
		}

		elseif($article['ctype'] == 'html')
			$tease = !empty($article['config']['settings']['teaser']) ? -1 : 0;

		elseif($article['ctype'] != 'php')
			$tease = $article['config']['settings']['teaser'];

		// article teaser set?
		if(!empty($tease))
		{
			$tmp = '
								<div id="short_'. $statID .'">
								'. PortaMx_Tease_posts($article['content'], $tease, '<div class="smalltext" style="text-align:'.(empty($context['right_to_left']) ? 'right' : 'left').';"><a id="href_short_'. $statID .'" href="'.$scripturl .'" style="padding: 0 5px;" onclick="ShowHTML(\''. $statID .'\')">'. $txt['pmx_readmore'] .'</a></div>') .'
								</div>';

			// if teased?
			if(!empty($context['pmx']['is_teased']))
			{
				$article['content'] = '
								<div id="full_'. $statID .'" style="display:none;">'. $article['content'] .'
									<div class="smalltext" style="text-align:'.(empty($context['right_to_left']) ? 'right' : 'left').';">
										<a id="href_full_'. $statID .'" href="'.$scripturl .'" style="padding: 0 5px;" onclick="ShowHTML(\''. $statID .'\')">'. $txt['pmx_readclose'] .'</a>
									</div>
								</div>'. $tmp;
			}
			unset($tmp);
		}

		// check for php code in html articles
		if($article['ctype'] == 'html')
		{
			$phpcount = preg_match_all('/(<\?)(php)(.*)\?>/Ums', $article['content'], $matches, PREG_SET_ORDER);
			if($phpcount != 0)
			{
				// remove duplicate code
				$cnt = $phpcount -1;
				for($i = 0; $i < $cnt; $i++) {
					if($matches[$i][0] == $matches[$i+1][0])
					{
						unset($matches[$i]);
						$cnt--;
					}
				}

				// create find/replace array,
				foreach($matches as $key => $phpevals)
				{
					$phpcode[$key] = '\';' . "\n". trim($phpevals[3]) ."\n". 'echo \'';
					$remove[$key] = $phpevals[0];
					$marker[$key] = '@['. $key .']@';
				}
				$article['content'] = str_replace($remove, $marker, $article['content']);

				// remove spaces, cr, lf before and after php code
				$start = 0;
				foreach($marker as $find)
				{
					$end = strpos($article['content'], $find, $start);
					$article['content'] = str_replace(substr($article['content'], $start, $end), trim(substr($article['content'], $start, $end)), $article['content']);
					$start = strpos($article['content'], $find, $start) + strlen($find);
				}

				// escape single quotes for php echo
				$article['content'] = str_replace("'", "\'", $article['content']);

				// put a echo arond for php eval and replace the marker with plain php code
				$article['content'] = "echo '". str_replace($marker, $phpcode, $article['content']) ."';";

				// cleanup
				unset($matches);
				unset($phpcode);
			}

			// Write out the content
			if($phpcount != 0)
				eval($article['content']);
			else
				echo $article['content'];
		}

		elseif($article['ctype'] == 'php')
		{
			// Check we have a show part
			if(preg_match('~\[\?pmx_showphp(.*)pmx_showphp\?\]~is', $article['content'], $match))
				eval($match[1]);

			// else write out the content
			else
				eval($article['content']);
		}

		else
			echo $article['content'];

		if(!empty($article['config']['settings']['showfooter']))
		{
			echo '
							<div style="clear:both;height:20px;"><hr />
								<div class="smalltext" style="float:'. (empty($context['right_to_left']) ? 'left' : 'right') .';">
									'. $txt['pmx_text_createdby'] .'<a href="'. $scripturl .'?action=profile;u='. $article['owner'] .'">'. $article['member_name'] .'</a>, '. timeformat($article['created']) .'
								</div>';

			if(!empty($article['updated']))
			{
				echo '
							<div class="smalltext" style="float:'. (empty($context['right_to_left']) ? 'right' : 'left') .';">
								'. $txt['pmx_text_updated'] . timeformat($article['updated']) .'
							</div>';
			}
			echo '
						</div>';
		}
	}
}
?>